package com.android.inger;


import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import com.android.inger.StatusReport;

public class LiftCheckerWidgetProvider extends AppWidgetProvider {
    private AlarmManager mAlarmManager;
    private Intent mIntent;
    private PendingIntent mPendingIntent;
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {        
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        //Log.v("", "onUpdate");
        if(mAlarmManager==null) {
            //Log.v(this.getClass().getName(), "Launching Clock Widget. Initiating...");
            mAlarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
            mIntent = new Intent();
            mIntent.setAction("com.android.inger.SECONDS_UPDATE");
            mPendingIntent = PendingIntent.getBroadcast(context, 0, mIntent, 0);
            mAlarmManager.setRepeating(AlarmManager.RTC, (long)0, (long)1000, mPendingIntent);
        }
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
		
		String station[][]=new String[100][5];
		StatusReport report=new StatusReport();
		report.create();
		int statusL;
		statusL=report.badLifts;	
		int statusS;
		statusS=report.badStairs;	
		station=report.statusReport;

	//station=StatusReport.start();
      
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
     rv.setTextViewText(R.id.output, "Defekte Stairs: "+statusS+"\nlifts "+statusL);
		if(statusL==0&&statusS==0)
			rv.setImageViewResource(R.id.status, R.drawable.lift_stairs_ok);
			if(statusL==0&&statusS>0)
        rv.setImageViewResource(R.id.status, R.drawable.lift_ok_stairs_bad);
        	if(statusL>0&&statusS>0)
        rv.setImageViewResource(R.id.status, R.drawable.lift_stairs_bad);
			if(statusL>0&&statusS==0)
        rv.setImageViewResource(R.id.status, R.drawable.lift_bad_stairs_ok);
		
		
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        ComponentName thisWidget = new ComponentName(context, LiftCheckerWidgetProvider.class);
        appWidgetManager.updateAppWidget(thisWidget, rv);
    }
    
    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        //Log.v(this.toString(), "Widget removed. Disabling now.");
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
        mIntent = new Intent();
        mIntent.setAction("com.android.inger.SECONDS_UPDATE");
        mPendingIntent = PendingIntent.getBroadcast(context, 0, mIntent, 0);
        alarmManager.cancel(mPendingIntent);        
    }
}  